package com.coba.killthispage;


import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author USER
 */
public class StarSprite extends Sprite {

    private int x = 0, y = 0, //current x/y 
            previous_x, previous_y; //last x/y
    private static final int MAN_WIDTH = 25;
    private static final int MAN_HEIGHT = 25;

    public StarSprite(Image image, int x, int y) {
        //Call sprite constructor
        super(image);
        this.x = x;
        this.y = y;

        this.setPosition(x, y);
    }

    public void moveLeft() {
        //if the man will not hit the left edge...
        if (x > 0) {
            saveXY();

            //if less than 3 from left, set to zero,
            //otherwise, subtract 3 from current location
            x = (x < 3 ? 0 : x - 3);
            setPosition(x, y);
        }
    }

    public void moveRight(int w) {
        // if the man will not hit the right edge
        if ((x + MAN_WIDTH) < w) {
            saveXY();

            // If current x plus width of ball goes over right side,
            // set to rightmost position. Otherwise add 3 to current location.
            x = ((x + MAN_WIDTH > w) ? (w - MAN_WIDTH) : x + 3);
            setPosition(x, y);
        }
    }

    public void moveUp() {
    // If the man will not hit the top edge...
        if (y > 0) {
            saveXY();
            // If less than 3 from top, set to zero,
            // otherwise, subtract 3 from current location.
            y = (y < 3 ? 0 : y - 3);
            setPosition(x, y);
        }
    }

    public void moveDown(int h) {
    // If the man will not hit the bottom edge...
        if ((y + MAN_HEIGHT) < h) {
            saveXY();
    // If current y plus height of ball goes past bottom edge,
    // set to bottommost position. Otherwise add 3 to current location.
            y = ((y + MAN_WIDTH > h) ? (h - MAN_WIDTH) : y + 3);
            setPosition(x, y);
        }
    }

    /*--------------------------------------------------
     * Save x and y, which are needed if collision is
     * detected.
     *-------------------------------------------------*/
    private void saveXY() {
    // Save last position
        previous_x = x;
        previous_y = y;
    }
    /*--------------------------------------------------
     * When a collision is detected, move back to
     * the previous x/y.
     *-------------------------------------------------*/

    public void restoreXY() {
        x = previous_x;
        y = previous_y;
        setPosition(x, y);
    }
}
